﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.Mvc;
using BLL.Security;
using BLL.Services;
using BLL.Services.Friends;
using BLL.Services.Friends.Listing;
using BLL.Services.Messages;
using BLL.Services.Users;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using Web.Infrastructure;
using Web.Infrastructure.Models;
using Web.Infrastructure.Security;
using Web.Models.Friends;

namespace Web.Controllers
{
    public partial class FriendsController : FrontendController
    {
        private readonly IFriendsService _friendsService;

        private readonly IUsersService _usersService;

        private readonly IMessagesService _messagesService;

        private readonly IEnvironmentContext _environmentContext;

        public FriendsController(IFriendsService friendsService, IUsersService usersService, IMessagesService messagesService, IEnvironmentContext environmentContext)
        {
            _friendsService = friendsService;
            _usersService = usersService;
            _messagesService = messagesService;
            _environmentContext = environmentContext;
        }

        [ListingAction("Login")]
        public virtual ActionResult FriendsListing(string userName, int page, int size, GridSortOptions sort, ListingFriendsFilter filter)
        {
            var user = _usersService.GetUserByEmailOrLogin(userName);
            var model = _friendsService.GetListingFriend(user.UserId, page, size, sort.Column, sort.Direction != SortDirection.Descending ? 0 : 1, filter);
            return BackBoneJson(model);
        }

        public virtual ActionResult FriendsListingTemplate()
        {
            return PartialView();
        }

        public virtual ActionResult FriendsListingEmptyTemplate()
        {
            return PartialView(MVC.Friends.Views.FriendsListingEmptyTemplate);
        }


        public virtual ActionResult DeleteFromFriend(int userId, int friendId)
        {
            var model = new BaseModel();
            try
            {
                _friendsService.Delete(friendId, userId);
            }
            catch (ServiceException exception)
            {
                ModelState.AddModelError(string.Empty, "Could not delete user");
                Trace.TraceError(exception.ToString());
            }
            model.Errors = GetErrors;
            if (ModelState.IsValid)
            {
                model.SuccessMessage = "User have been successfully deleted.";
            }

            return BackBoneJson(model);
        }


        public virtual ActionResult AddToFriends(string friendLogin)
        {
            var model = new BaseModel();
            var user = _environmentContext.User;

            var friend = _usersService.GetUserByEmailOrLogin(friendLogin);

            try
            {
                _friendsService.SetFriend(user.UserId, friend.UserId);
            }

            catch (ServiceException exception)
            {
                ModelState.AddModelError(string.Empty, "Could not add user to friends");
                Trace.TraceError(exception.ToString());
            }


            return BackBoneJson(model);
        }

        public virtual ActionResult GetFriends(string login)
        {
            var user = _usersService.GetUserByEmailOrLogin(login);
            var friends = _friendsService.GetFriendsByUserId(user.UserId);
            return BackBoneJson(friends);
        }

        public virtual ActionResult GetFriendsRequest(string login)
        {
            var user = _usersService.GetUserByEmailOrLogin(login);
            var friends = _friendsService.GetFriendsRequests(user.UserId);
            return BackBoneJson(friends);
        }

        [ListingAction("Login")]
        public virtual ActionResult FriendsIsAcceptedListing(int page, int size, GridSortOptions sort, FriendsIsAcceptedFilter filter)
        {
            var user = _environmentContext.User;
            var model = _friendsService.GetFriendIsAcceptedListing(user.UserId,page,size,sort.Column,sort.Direction != SortDirection.Descending ? 0: 1,filter);
            model.UserId = user.UserId;
            return BackBoneJson(model);
        }

        public virtual ActionResult FriendsIsAcceptedListingTemplate()
        {
            return PartialView(MVC.Friends.Views.FriendsIsAcceptedListingTemplate);
        }


        public virtual ActionResult FriendsIsAcceptedListingEmptyTemplate()
        {
            return PartialView(MVC.Friends.Views.FriendsIsAcceptedListingEmptyTemplate);
        }

        public virtual ActionResult SocialFriendsTemplate()
        {
            return PartialView(MVC.Friends.Views.SocialFriendsTemplate);
        }

        public virtual ActionResult SocialFriendsRequestTemplate()
        {
            return PartialView(MVC.Friends.Views.SocialFriendsRequestTemplate);
        }

        [HttpGet]
        public virtual ActionResult SearchFriend(SearchFriendModel model)
        {
            var friend = _usersService.GetUserByEmailOrLogin(model.SearchField);
            return BackBoneJson(friend);
        }
    }
}
